#include "gd32f30x.h"
#include "string.h"
#include "stdint.h"
#include "stdio.h"
#include "DevicesUart.h"
#include "DevicesCAN.h"
#include "saeJ1939.h"
#include "GBT27930Pgn.h"
#include "taskSaeJ1939.h"


/* 任务句柄 */
MrTaskType *g_ptypeMrTaskJ1939;

/* 消息队列句柄 */
MrMessageType *g_ptypeMrMessageJ1939;


void vMrTaskJ1939(uint32_t uiParameters)
{
    CanPackType typeCanPack;
    J1939PduType typeJ1939Pdu = {0};

    cMrMessageCreate(&g_ptypeMrMessageJ1939, MR_MESSAGE_TYPE_FIFO,   sizeof(uint32_t), 8, 0);

    /* PGN 注册 */
    cGBT27930_PGNxFuctionInit();

    while(1)
    {
        cMessageRead(g_ptypeMrMessageJ1939, &uiParameters, -1);

        /* 读取并解析 上位机 发送过来的数据 */
        while(cCanxReceiveDatas(CAN0, &typeCanPack, sizeof(typeCanPack)) == 0)
        {
            typeJ1939Pdu.P  = typeCanPack.id >> 26;
            typeJ1939Pdu.DP = typeCanPack.id >> 24;
            typeJ1939Pdu.PF = typeCanPack.id >> 16;
            typeJ1939Pdu.PS = typeCanPack.id >> 8;
            typeJ1939Pdu.SA     = typeCanPack.id;
            typeJ1939Pdu.length = typeCanPack.length;
            memcpy(typeJ1939Pdu.datas, typeCanPack.datas, typeCanPack.length);

            cJ1939PduReceive(&g_typeJ1939Packs[0], &typeJ1939Pdu);
        }

        /* 读取并解析 上位机 发送过来的数据 */
        while(cCanxReceiveDatas(CAN1, &typeCanPack, sizeof(typeCanPack)) == 0)
        {
            typeJ1939Pdu.P  = typeCanPack.id >> 26;
            typeJ1939Pdu.DP = typeCanPack.id >> 24;
            typeJ1939Pdu.PF = typeCanPack.id >> 16;
            typeJ1939Pdu.PS = typeCanPack.id >> 8;
            typeJ1939Pdu.SA     = typeCanPack.id;
            typeJ1939Pdu.length = typeCanPack.length;
            memcpy(typeJ1939Pdu.datas, typeCanPack.datas, typeCanPack.length);

            cJ1939PduReceive(&g_typeJ1939Packs[1], &typeJ1939Pdu);
        }
    }
}
